﻿/*****************************************************
 * WindowManager                                     *
 * Made by Niklas Plüm (l4nm4n@users.sourceforge.net)*
 *****************************************************/

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using VisualPureBasicIDE.CoreGUI.Properties;
using VisualPureBasicIDE.FormDesigner;
using WeifenLuo.WinFormsUI.Docking;

namespace VisualPureBasicIDE.CoreGUI
{
    /// <summary>
    /// This class handles the most reusable windows of the IDE (lazy initialization)
    /// </summary>
    public class WindowManager
    {
        /// <summary>
        /// Returns a static instance of formBuildOutput
        /// </summary>
        public static BuildOutputForm BuildOutputWindow
        {
            get
            {
                return InstanceManager.GetInstance(typeof(BuildOutputForm)) as BuildOutputForm;
            }
        }

        /// <summary>
        /// Returns a static instance of formConsole
        /// </summary>
        public static ConsoleForm Console
        {
            get
            {
                return InstanceManager.GetInstance(typeof(ConsoleForm)) as ConsoleForm;
            }
        }

        /// <summary>
        /// Returns a static instance of formObjectInspector
        /// </summary>
        public static ObjectInspector ObjectInspector
        {
            get
            {
                return InstanceManager.GetInstance(typeof(ObjectInspector)) as ObjectInspector;
            }
        }

        /// <summary>
        /// Returns a static instance of formToolbox
        /// </summary>
        public static Toolbox Toolbox
        {
            get
            {
                return InstanceManager.GetInstance(typeof(Toolbox)) as Toolbox;
            }
        }

        /// <summary>
        /// Returns a static instance of formSolutionExplorer
        /// </summary>
        public static SolutionExplorerForm SolutionExplorer
        {
            get
            {
                return InstanceManager.GetInstance(typeof(SolutionExplorerForm)) as SolutionExplorerForm;
            }
        }

        /// <summary>
        /// Initializes the layout of the dockPanel (creates a new or loads it from a file)
        /// </summary>
        internal static void InitializeLayout()
        {
            if (File.Exists(InvariantStrings.DockLayoutFilePath))
            {
                // DockLayout.xml file already exists; now load it
                ApplicationManager.MainForm.DockPanel.LoadFromXml(InvariantStrings.DockLayoutFilePath, InitializeWindows); // Performance issue!
            }
            else
            {
                // The file doesn't exist, so create a new one
                BuildOutputWindow.DockPanel = ApplicationManager.MainForm.DockPanel;
                BuildOutputWindow.DockState = DockState.DockBottom;
                ObjectInspector.DockPanel = ApplicationManager.MainForm.DockPanel;
                ObjectInspector.DockState = DockState.DockRightAutoHide;
                Toolbox.DockPanel = ApplicationManager.MainForm.DockPanel;
                Toolbox.DockState = DockState.DockLeftAutoHide;
                BuildOutputWindow.Show();
                ObjectInspector.Show();
                Toolbox.Show();
                ApplicationManager.MainForm.DockPanel.SaveAsXml(InvariantStrings.DockLayoutFilePath);
            }
        }

        internal static void SaveLayout()
        {
            try
            {
                Settings.Default.FormMainSize = ApplicationManager.MainForm.Size;
                Settings.Default.Save();

                ApplicationManager.MainForm.DockPanel.SaveAsXml(InvariantStrings.DockLayoutFilePath);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }

        /// <summary>
        /// Aligns the main window (MainForm) to the primary screen or loads the size from the settings
        /// </summary>
        internal static void AlignMainWindowToScreen()
        {
            if (Settings.Default.FormMainSize == Size.Empty)
            {
                // Form size setting doesn't exist, so create one now
                var workingArea = Screen.GetWorkingArea(ApplicationManager.MainForm);

                ApplicationManager.MainForm.Width = (int)Math.Round(workingArea.Width / 1.5, 0);
                ApplicationManager.MainForm.Height = (int)Math.Round(workingArea.Height / 1.2, 0);

                Settings.Default.FormMainSize = ApplicationManager.MainForm.Size;
                Settings.Default.Save();
            }
            else
            {
                // Form size setting exists, so load it
                ApplicationManager.MainForm.Size = Settings.Default.FormMainSize;
            }
        }

        /// <summary>
        /// This restores the windows and their states
        /// </summary>
        /// <param name="value">The windows represented by a string</param>
        /// <returns>Returns the wished window. If it's unknown it returns "null".</returns>
        internal static IDockContent InitializeWindows(string value)
        {
            // Handling each type of possible windows

            // NOTE: switch...case is not possible here
            if (value == typeof(BuildOutputForm).ToString())
            {
                BuildOutputWindow.DockPanel = ApplicationManager.MainForm.DockPanel;
                return BuildOutputWindow.DockHandler.Content;
            }

            if (value == typeof(ObjectInspector).ToString())
            {
                ObjectInspector.DockPanel = ApplicationManager.MainForm.DockPanel;
                return ObjectInspector.DockHandler.Content;
            }

            if (value == typeof(Toolbox).ToString())
            {
                Toolbox.DockPanel = ApplicationManager.MainForm.DockPanel;
                return Toolbox.DockHandler.Content;
            }

            if (value == typeof(SolutionExplorerForm).ToString())
            {
                SolutionExplorer.DockPanel = ApplicationManager.MainForm.DockPanel;
                return SolutionExplorer.DockHandler.Content;
            }
            return null;
        }
    }
}